﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.datatables;
using l1cs.server.model.npc;
using l1cs.server.serverpackets;
using l1cs.server.model.npc.action;

namespace l1cs.server.clientpackets
{
    public class C_NPCTalk : ClientBasePacket
    {
        private const String _C_NPC_TALK = "[C] C_NPCTalk";
        private static Logger _log = Logger.getLogger( typeof( C_NPCTalk ).FullName );

        public C_NPCTalk( byte[] abyte0, ClientThread client )
            : base( abyte0 )
        {
            int objid = readD();
            L1Object obj = L1World.getInstance().findObject( objid );
            L1PcInstance pc = client.getActiveChar();
            if ( obj != null && pc != null )
            {
                L1NpcAction action = NpcActionTable.getInstance().get( pc, obj );
                if ( action != null )
                {
                    L1NpcHtml html = action.execute( "", pc, obj, new byte[0] );
                    if ( html != null )
                    {
                        pc.sendPackets( new S_NPCTalkReturn( obj.getId(), html ) );
                    }
                    return;
                }
                obj.onTalkAction( pc );
            }
            else
            {
                _log.severe( "オブジェクトが見つかりません objid=" + objid );
            }
        }

        override
        public String getType()
        {
            return _C_NPC_TALK;
        }
    }
}